package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@DubboService
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderMapper orderMapper;
    private static Object a = 2;

    @Override
    public Order add(Order order) {
        //1.查询当天是否可以预约--->当天是否有预约信息
        OrderSetting orderSetting = orderSettingMapper.selectByDate(order.getOrderDate());
        if (orderSetting == null) {
            throw new BusinessException("当天不可预约！");
        }
        //可预约人数<=已预约人数
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            throw new BusinessException("当天预约人数已满，请选择其他日期！");
        }
        //2.查询该用户在当天是否已经预约过该套餐体检信息
        Order order1 = new Order();
        order1.setMemberId(order.getMemberId());
        order1.setOrderDate(order.getOrderDate());
        order1.setSetmealId(order.getSetmealId());
        int count = orderMapper.selectCount(order1);
        if (count > 0) {
            throw new BusinessException("当天已经预约过该体检套餐，省点钱吧！");
        }
        //3.通过后新增订单数据
        orderMapper.insertSelective(order);
        //4.维护t_ordersetting表---》已预约人数+1

        synchronized (a) {
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingMapper.updateByPrimaryKeySelective(orderSetting);
        }
        return order;
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> map = orderMapper.findById(id);
        return map;
    }

    @Override
    public Set<Member> findTomMember() {
        return orderMapper.findTomMember();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //分页查询起始
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //获取example对象
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        String orderStatus = queryPageBean.getOrderStatus();

        if (!StringUtils.isEmpty(orderStatus)){
            criteria.orLike("orderStatus", "%" + orderStatus + "%");
        }
        //判断查询条件是否为空,模糊查询
        if (!StringUtils.isEmpty(queryString)) {
            criteria.andLike("name", "%" + queryString + "%");
        }
        Page<Order> page = (Page<Order>) orderMapper.selectByExample(example);
        List<Order> orders = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, orders);
    }

    /**
     * @Author: lenovo
     * @Date: 23:17 2021/5/11
     * @Parms [id, orderStatus]
     * @Description: 修改预约状态为驳回
     */
    @Override
    public void delete(Integer id, String status) {
        Order order = new Order();
        order.setId(id);
        order.setOrderStatus(status);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void edit(Integer id, String status) {
        Order order = new Order();
        order.setId(id);
        order.setOrderStatus(status);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<Order> findAll() {
        List<Order> orders = orderMapper.selectAll();
        return orders;
    }

    @Override
    public Order findOrderById(Integer id) {
        Order order = new Order();
        order.setId(id);
        Order order1 = orderMapper.selectByPrimaryKey(order);
        return order1;
    }
}
